package com.zg.autoflow.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zg.autoflow.service.DiyColumnService;
import com.zg.autoflow.vo.ColumnsExtendsVo;
import com.zg.autoflow.vo.DiyColumnVo;
import com.zg.common.core.A;
import com.zg.common.core.dao.autoflow.dao.AutoflowExtendsDao;
import com.zg.common.core.dao.autoflow.dao.AutoflowMainDao;
import com.zg.common.core.dao.autoflow.dao.AutoflowStepDao;
import com.zg.common.core.dao.autoflow.entity.AutoflowExtends;
import com.zg.common.core.dao.autoflow.entity.AutoflowMain;
import com.zg.common.core.dao.autoflow.entity.AutoflowStep;
import com.zg.common.core.dao.autoform.DiyColumn;
import com.zg.common.core.dao.autoform.DiyColumnDao;
import com.zg.common.core.dao.autoform.DiyForm;
import com.zg.common.core.dao.autoform.DiyFormDao;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author zg
*/
@Service
@RequiredArgsConstructor
public class DiyColumnServiceImpl implements DiyColumnService {

    private final AutoflowExtendsDao autoflowExtendsDao;
    private final DiyColumnDao diyColumnDao;
    private final AutoflowMainDao autoflowMainDao;
    private final AutoflowStepDao autoflowStepDao;
    private final DiyFormDao diyFormDao;

    @Override
    public List<DiyColumnVo> getColumns(Long formId) {
        List<DiyColumn> diyColumns = diyColumnDao.list(new LambdaQueryWrapper<DiyColumn>().eq(DiyColumn::getFormId, formId));
        if (diyColumns != null && !diyColumns.isEmpty()) {
            List<DiyColumnVo> diyColumnVos = diyColumns.stream().map(t -> {
                DiyColumnVo diyColumnVo = new DiyColumnVo();
                BeanUtil.copyProperties(t, diyColumnVo);
                return diyColumnVo;
            }).collect(Collectors.toList());
            return diyColumnVos;
        }
        return Collections.emptyList();
    }

    @Override
    public List<ColumnsExtendsVo> getColumnsExtendsByFormId(Long formId) {
        List<DiyColumn> diyColumns = diyColumnDao.list(new LambdaQueryWrapper<DiyColumn>().eq(DiyColumn::getFormId, formId));
        if (diyColumns == null || diyColumns.isEmpty()) {
            return Collections.emptyList();
        }

        // 获取继承关系
        List<AutoflowExtends> autoflowExtends = autoflowExtendsDao.list(new LambdaQueryWrapper<AutoflowExtends>().eq(AutoflowExtends::getSourceFormId, formId));
        if (autoflowExtends == null || autoflowExtends.isEmpty()) {
            return diyColumns.stream().map(t -> {
                ColumnsExtendsVo columnsExtendsVo = new ColumnsExtendsVo();
                columnsExtendsVo.setColumnId(t.getId());
                columnsExtendsVo.setSourceField(t.getName());
                columnsExtendsVo.setSourceColumn(t.getCode());
                return columnsExtendsVo;
            }).collect(Collectors.toList());
        }

        Map<String, AutoflowExtends> autoflowExtendsMap = autoflowExtends.stream().collect(Collectors.toMap(AutoflowExtends::getSourceColumn, Function.identity(), (v1, v2) -> v1));

        // 获取流程信息
        Set<Long> flowIds = autoflowExtends.stream().map(AutoflowExtends::getTargetFlowId).collect(Collectors.toSet());
        List<AutoflowMain> autoflowMains = flowIds.isEmpty() ? new ArrayList<>() : autoflowMainDao.listByIds(flowIds);
        Map<Long, AutoflowMain> autoflowMainMap = autoflowMains.stream().collect(Collectors.toMap(AutoflowMain::getId, Function.identity(), (v1, v2) -> v1));

        // 获取环节信息
        Map<Long, Map<Long, AutoflowStep>> autoflowMainStepMap = new HashMap<>();
        for (Long flowId : flowIds) {
            if (flowId != null) {
                List<AutoflowStep> autoflowSteps = autoflowStepDao.list(new LambdaQueryWrapper<AutoflowStep>().eq(AutoflowStep::getFlowId, flowId));
                if (autoflowSteps != null && !autoflowSteps.isEmpty()) {
                    Map<Long, AutoflowStep> autoflowStepMap = autoflowSteps.stream().collect(Collectors.toMap(AutoflowStep::getId, Function.identity(), (v1, v2) -> v1));
                    autoflowMainStepMap.put(flowId, autoflowStepMap);
                }
            }
        }

        // 获取表单信息
        Set<Long> formIds = autoflowExtends.stream().map(AutoflowExtends::getTargetFormId).collect(Collectors.toSet());
        List<DiyForm> diyForms = formIds.isEmpty() ? new ArrayList<>() : diyFormDao.listByIds(formIds);
        Map<Long, DiyForm> diyFormMap = diyForms.stream().collect(Collectors.toMap(DiyForm::getId, Function.identity(), (v1, v2) -> v1));

        return diyColumns.stream().map(t -> {
            ColumnsExtendsVo columnsExtendsVo = new ColumnsExtendsVo();
            columnsExtendsVo.setColumnId(t.getId());
            columnsExtendsVo.setSourceField(t.getName());
            columnsExtendsVo.setSourceColumn(t.getCode());

            AutoflowExtends autoflowExtend = autoflowExtendsMap.get(t.getCode());
            if (autoflowExtend != null) {
                Long extendsId = autoflowExtend.getId();
                Long targetFlowId = autoflowExtend.getTargetFlowId();
                Long targetStepId = autoflowExtend.getTargetStepId();
                Long targetFormId = autoflowExtend.getTargetFormId();

                columnsExtendsVo.setExtendsId(extendsId);

                AutoflowMain flowMain = targetFlowId != null ? autoflowMainMap.get(targetFlowId) : null;
                if (flowMain != null) {
                    columnsExtendsVo.setTargetFlowCode(flowMain.getCode());
                    columnsExtendsVo.setTargetFlowName(flowMain.getName());
                }

                if (targetFlowId != null && targetStepId != null) {
                    Map<Long, AutoflowStep> stepMap = autoflowMainStepMap.get(targetFlowId);
                    if (stepMap != null) {
                        AutoflowStep step = stepMap.get(targetStepId);
                        if (step != null) {
                            columnsExtendsVo.setTargetStepCode(step.getStepNo());
                            columnsExtendsVo.setTargetStepName(step.getStepName());
                        }
                    }
                }

                if (targetFormId != null) {
                    DiyForm form = diyFormMap.get(targetFormId);
                    if (form != null) {
                        columnsExtendsVo.setTargetFormName(form.getName());
                    }
                }

                columnsExtendsVo.setTargetField(autoflowExtend.getTargetField());
                columnsExtendsVo.setTargetColumn(autoflowExtend.getTargetColumn());
                columnsExtendsVo.setExtendType(autoflowExtend.getExtendType());
            }
            return columnsExtendsVo;
        }).collect(Collectors.toList());
    }
}
